package cn.edu.nuc.thrid.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.nuc.thrid.model.BuyMes;
import cn.edu.nuc.thrid.model.Cart;
import cn.edu.nuc.thrid.model.CountMoney;
import cn.edu.nuc.thrid.model.Goods;
import cn.edu.nuc.thrid.services.interfaces.CartService;
import cn.edu.nuc.thrid.services.interfaces.GoodsService;

@Controller
@RequestMapping("/cart")
public class CartController {

	@Autowired
	private CartService service;

	/**
	 * 添加购物车
	 * 
	 * @param cart
	 * @return 视图
	 */
	@RequestMapping(value = "/add")
	public String add(Cart cart, Model model) {

		// System.out.println("cart:" + cart);

		Integer result = service.addCart(cart);

		if (result == -1) {

			model.addAttribute("mes", "库存不足");
			return "user/close";
		}
		if (result > 0) {
			model.addAttribute("mes", "添加成功");
			return "user/close";
		} else {

			return "error";
		}
	}

	@RequestMapping(value = "/cart")
	public String cart(String zybuserid, Model model) {

		List<BuyMes> list = service.cartList(zybuserid);
		System.out.println(list);
		int count = 0;
		double money = 0;

		CountMoney cm;
		if (list.size() == 0) {
			cm = new CountMoney(0, 0.00);
			model.addAttribute("cm", cm);
			model.addAttribute("errors", "购物车列表为空或者商品下架");
			return "user/cartGoods";
		} else {
			for (int i = 0; i < list.size(); i++) {

				BuyMes b = list.get(i);
				count += b.getNumber();
				money += Double.parseDouble(b.getPrice()) * b.getNumber();
			}

			cm = new CountMoney(count, money);
			model.addAttribute("cm", cm);
			model.addAttribute("goodslist", list);
			return "user/cartGoods";
		}

	}

	/**
	 * 更新库存
	 * 
	 * @param zybuserid
	 * @return
	 */
	@RequestMapping(value = "/result", method = RequestMethod.GET)
	public String result(String zybuserid,Model model) {

		List<BuyMes> list = service.cartList(zybuserid);
		System.out.println("list;" + list);
		Goods goods = new Goods();
		int num = 0;
		for (int i = 0; i < list.size(); i++) {

			BuyMes mes = list.get(i);
			Integer quantity = mes.getNumber();
			goods.setZybgoodid(String.valueOf(mes.getGoodid()));
			goods.setZybquantity(quantity);
			System.out.println("goods:" + goods);
			Integer result = service.result(goods);
			if(result == -1){
				
				model.addAttribute("mes", "您要结算的有库存不足");
				return "user/close";
			}
			num = num + result;
			System.out.println("result:" + result);
		}

		if (num == list.size()) {

			service.deletecart(zybuserid);
			return "redirect:/user/userpagelist";
		} else {

			return "error";
		}

	}

	/**
	 * 批量
	 * @param zybgoodid
	 * @param zybuserid
	 * @return
	 */
	@RequestMapping(value = "/results", method = RequestMethod.POST)
	public  String results(Integer[] zybgoodid, Integer zybuserid,Model model) {
		//System.out.println(zybgoodid.length + "," + zybuserid + ",goods:");

		if(zybgoodid == null ||zybgoodid.length==0){
			model.addAttribute("err", "您没有选择要买的东西");
			return "forward:/cart/cart";
		}
		for (int i = 0; i < zybgoodid.length; i++) {
			
			Integer result = service.results(zybgoodid[i], zybuserid);
			if(result == -1){
				
				model.addAttribute("mes", "您要结算的有库存不足");
				return "user/close";
			}
		}
		service.deletecarts(zybgoodid, zybuserid);
		model.addAttribute("succ", "结算成功");
		return "redirect:/user/userpagelist";
	}
}
